<script setup lang="ts">
import { getHomeGoodsGuessLikeAPI } from '@/services/home'
import type { PageParams } from '@/types/common'
import type { GuessItem } from '@/types/home'
import { ref } from 'vue'
// 页数
// 常用泛型工具笔记： https://www.yuque.com/tangbohudianwenxiang-5uhjk/aa37wn/ped1u297z5crzehp
const params = ref<Required<PageParams>>({
  page: 1,
  pageSize: 10
})

const isFinish = ref(false) // 是否结束

// 获取数据
const list = ref<GuessItem[]>([])
const getList = async () => {
  // 如果已经结束，则不进行操作
  if (isFinish.value) {
    return uni.showToast({ icon: 'none', title: '没有更多数据~' })
  }

  // 获取数据
  const res = await getHomeGoodsGuessLikeAPI(params.value)
  list.value.push(...res.result.items)

  // 分页条件
  if (params.value.page < res.result.pages) {
    // 页码累加
    params.value.page++
  } else {
    isFinish.value = true
  }
}
getList()

// 重置数据
const resetList = () => {
  list.value = []
  isFinish.value = false
  params.value.page = 1
}

// 暴露给父组件
defineExpose({
  getGuessList: getList,
  resetGuessList: resetList
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in list"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name"> {{ item.name }} </view>
      <view class="price">
        <text class="small">¥</text>
        <text>899.00</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text"> {{ isFinish ? '没有更多数据~' : '正在加载...' }} </view>
</template>

<style lang="scss">
@import '@/components/styles/xtxGuess.scss';
</style>
